package com.sml.contorller;

import com.sml.pojo.Article;
import com.sml.pojo.PageBean;
import com.sml.pojo.Result;
import com.sml.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 新增文章
     *
     * @return 响应结果对象 Result<String>
     */
    @PostMapping
    public Result<String> add(@RequestBody @Validated Article article) {
        articleService.add(article);
        return Result.success();
    }

    /**
     * 分页查询文章列表
     *
     * @param pageNum    页码
     * @param pageSize   每页记录数
     * @param categoryId 文章分类id
     * @param state      文章状态
     * @return 响应结果对象 PageBean<Article>
     */
    @GetMapping
    public Result<PageBean<Article>> list(Integer pageNum, Integer pageSize,
                                          @RequestParam(required = false) Integer categoryId,
                                          @RequestParam(required = false) String state) {
        PageBean<Article> pb = articleService.list(pageNum, pageSize, categoryId, state);
        return Result.success(pb);
    }

}
